package com.hn658.base.message.notification.notify.dao.redis.impl;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.hn658.base.message.common.enums.NotifyType;
import com.hn658.base.message.common.utils.RedisKeyHelper;
import com.hn658.base.message.notification.notify.dao.redis.INotifyRedisDAO;
import com.hn658.base.message.notification.notify.entity.TalkerEO;
import com.hn658.framework.dataaccess.redis.AbstractRedisDAO;
import com.hn658.framework.shared.utils.JsonMapper;


@Component
public class NotifyRedisDAOImpl extends AbstractRedisDAO<TalkerEO> implements INotifyRedisDAO{

	@Autowired
    @Qualifier("redisTemplate")
    @Override
    public void setTemplate(StringRedisTemplate template) {
        super.template = template;
    }
	
	JsonMapper js = JsonMapper.nonDefaultMapper();

	@Override
	public void saveTalker(String appId, String uid, TalkerEO talker) {
		String talkerStr = js.toJson(talker);
		valueOps.set(RedisKeyHelper.talkerKeyById(appId, uid),talkerStr);
	}

	@Override
	public TalkerEO getTalker(String appId, String uid) {
		String talkerEOStr = valueOps.get(RedisKeyHelper.talkerKeyById(appId, uid));
		TalkerEO talkerEO =null;
		if(StringUtils.isNotBlank(talkerEOStr)){
			talkerEO= js.fromJson(talkerEOStr, TalkerEO.class);
		}
		return talkerEO;
	}

	@Override
	public void setNotiReadCount(String appId, String uid,String notifyId,int count) {
		BoundHashOperations<String, String, String> userOps = template
                .boundHashOps(RedisKeyHelper.readCountKeyById(appId,uid));
		userOps.put(notifyId, count+"");
		
	}

	@Override
	public int getNotiReadCount(String appId, String uid,String notifyId) {
		BoundHashOperations<String, String, String> userOps = template
                .boundHashOps(RedisKeyHelper.readCountKeyById(appId,uid));
		int count=0;
		if(StringUtils.isNotBlank(userOps.get(notifyId))){
			count= Integer.parseInt(userOps.get(notifyId));
		}
		return count;
	}

	
}
